<template>
  <div>
    <el-dialog :before-close="cancel" title="编辑会员" :visible.sync="info.isShow" center>
      <el-form ref="member" :model="memberinfo" :rules="rules">
        <el-form-item prop="uid" label="手机号" :label-width="formLabelWidth">
          <el-input v-model="memberinfo.phone" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item prop="nickname" label="昵称" :label-width="formLabelWidth">
          <el-input v-model="memberinfo.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="memberinfo.password" autocomplete="off"></el-input>
          <p>留空则是不修改</p>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="memberinfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="confirm" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//引入封装好的接口
import { getMemberEdit, getMemberInfo } from "../../request/api";
import { mapGetters, mapActions } from "vuex";
export default {
  data() {
    return {
      memberinfo: {
        uid: 0,
        nickname: "", //昵称
        phone: "", //手机号
        password: "", //密码
        status: 1 //状态1正常2禁用
      },
      formLabelWidth: "120px",
      rules: {
        uid: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        nickname: [
          { required: true, message: "请输入昵称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" }
        ]
      }
    };
  },
  props: ["info"],
  mounted() {
    if (this.memberlist.length == 0) {
      this.getMemberList;
    }
  },
  computed: {
    ...mapGetters({
      memberlist: "member/getMemberList"
    })
  },
  methods: {
    ...mapActions({
      getMember: "member/getMemberListAction"
    }),
    //封装一个关闭弹框的事件
    cancel() {
      this.$emit("cancel", false);
      //当关闭弹框就调用重置事件
      this.reset();
    },
    //清空输入的表单内容以及相关验证
    reset() {
      this.memberinfo = {
        uid: 0,
        nickname: "", //昵称
        phone: "", //手机号
        password: "", //密码
        status: 1 //状态1正常2禁用
      };
    },
    //创建一个查询一条数据事件
    lookup(uid) {
      //调用查询一条数据事件
      getMemberInfo({ uid }).then(res => {
        console.log(res, "一条会员");
        if (res.code == 200) {
          this.memberinfo = res.list;
          this.memberinfo.uid = uid;
          this.memberinfo.password = "";
        }
      });
    },
    //封装一个确定事件
    confirm() {
      //先验证
      this.$refs.member.validate(val => {
        if (val) {
          console.log(this.memberinfo,'表单输入');
          //验证成功之后，调用编辑接口
          if (this.info.isAdd) {
            // 调用编辑接口
            getMemberEdit(this.memberinfo).then(res => {
              if (res.code == 200) {
                this.$message.success(res.msg);
                //关闭弹框
                this.cancel();
                //调用列表接口
                this.getMemberList();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        } else {
          console.log("验证失败");
        }
      });
    }
  }
};
</script>

<style scoped></style>
